from PIL import Image

def resize_image(image_path, width, height, output_path=None):
    """
    修改图片分辨率尺寸

    :param image_path: str, 原图片路径
    :param width: int, 目标宽度
    :param height: int, 目标高度
    :param output_path: str, 输出路径 (可选)，默认覆盖原图
    """
    # 打开图片
    with Image.open(image_path) as img:
        resized_img = img.resize((width, height), Image.LANCZOS)

        # 如果没有指定输出路径，则覆盖原文件
        if not output_path:
            output_path = image_path

        # 保存调整后的图片
        resized_img.save(output_path)
        print(f"图片已保存到: {output_path}")

# 使用示例
resize_image("img/1.jpg", 2480, 3508, "to_img/1.jpg")
